﻿package com.mui.controls
{
    import com.gengine.core.*;
    import com.gengine.global.*;
    import com.mui.core.*;
    import com.mui.skins.*;
    import com.mui.utils.*;
    import fl.controls.*;
    import fl.controls.listClasses.*;
    import fl.core.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class GTileList extends TileList implements IFrUI
    {
        private var _verticalGap:Number = 0;
        private var _horizontalGap:Number = 0;
        private var _arrVGap:Array;
        private var _arrHGap:Array;
        public var isClickedUpdate:Boolean = true;
        public var isChangeUpdate:Boolean = true;
        public var isShowVerticalScrollBar:Boolean = false;
        private var _isCenter:Boolean;
        private var _initialX:int;
        private var _isDisposed:Boolean;
        private var _styleName:String;
        private var _isStyleChange:Boolean = false;
        private var _isUseFixed:Boolean = false;
        private var _isDraw:Boolean = false;

        public function GTileList()
        {
            this.tabChildren = false;
            return;
        }// end function

        public function set horizontalGap(param1:Number) : void
        {
            this._horizontalGap = param1;
            return;
        }// end function

        public function get horizontalGap() : Number
        {
            return this._horizontalGap;
        }// end function

        public function set verticalGap(param1:Number) : void
        {
            this._verticalGap = param1;
            return;
        }// end function

        public function get verticalGap() : Number
        {
            return this._verticalGap;
        }// end function

        public function set center(param1:int) : void
        {
            this._isCenter = true;
            this._initialX = param1;
            return;
        }// end function

        public function setVerticalGapAt(param1:int, param2:Number) : void
        {
            if (this._arrVGap == null)
            {
                this._arrVGap = [];
            }
            this._arrVGap[param1] = param2;
            return;
        }// end function

        public function setHorizontalGapAt(param1:int, param2:Number) : void
        {
            if (this._arrHGap == null)
            {
                this._arrHGap = [];
            }
            this._arrHGap[param1] = param2;
            return;
        }// end function

        private function getTotalGap(param1:int, param2:Array, param3:int) : int
        {
            var _loc_4:int = 0;
            if (param2 == null)
            {
                return param1 * param3;
            }
            var _loc_5:int = 0;
            while (_loc_5 < param1)
            {
                
                if (param2[_loc_5] == null)
                {
                    _loc_4 = _loc_4 + param3;
                }
                else
                {
                    _loc_4 = _loc_4 + int(param2[_loc_5]);
                }
                _loc_5++;
            }
            return _loc_4;
        }// end function

        public function get styleName() : String
        {
            return this._styleName;
        }// end function

        public function set styleName(param1:String) : void
        {
            if (this._styleName != param1)
            {
                this._styleName = param1;
                invalidate(InvalidationType.STYLES);
                this._isStyleChange = true;
            }
            return;
        }// end function

        final override protected function draw() : void
        {
            if (this.isDisposed)
            {
                return;
            }
            if (isInvalid(InvalidationType.STYLES))
            {
                if (this._isStyleChange)
                {
                    SkinManager.setComponentStyle(this, this._styleName);
                    this._isStyleChange = false;
                }
            }
            this._isDraw = true;
            super.draw();
            this._isDraw = false;
            if (this._isCenter)
            {
                this.x = this._initialX + (this.width - this.dataProvider.length * this.columnWidth) / 2;
            }
            return;
        }// end function

        override protected function drawLayout() : void
        {
            super.drawLayout();
            if (vScrollBar || this.isShowVerticalScrollBar)
            {
                _verticalScrollBar.x = (columnWidth + this.horizontalGap) * columnCount;
                _verticalScrollBar.visible = true;
                _verticalScrollBar.y = contentPadding;
                _verticalScrollBar.height = availableHeight;
                this.addChild(_verticalScrollBar);
            }
            return;
        }// end function

        public function set useFixed(param1:Boolean) : void
        {
            this._isUseFixed = param1;
            return;
        }// end function

        override protected function drawList() : void
        {
            var _loc_1:uint = 0;
            var _loc_2:uint = 0;
            var _loc_3:Object = null;
            var _loc_4:ICellRenderer = null;
            var _loc_7:Number = NaN;
            var _loc_11:uint = 0;
            var _loc_12:uint = 0;
            var _loc_15:Dictionary = null;
            var _loc_16:uint = 0;
            var _loc_17:uint = 0;
            var _loc_18:uint = 0;
            var _loc_19:uint = 0;
            var _loc_20:Boolean = false;
            var _loc_21:String = null;
            var _loc_22:Object = null;
            var _loc_23:Object = null;
            var _loc_24:Sprite = null;
            var _loc_25:String = null;
            var _loc_26:UIComponent = null;
            var _loc_5:* = rowCount;
            var _loc_6:* = columnCount;
            _loc_7 = columnWidth;
            var _loc_8:* = rowHeight;
            var _loc_9:Number = 0;
            var _loc_10:Number = 0;
            var _loc_27:* = contentPadding;
            listHolder.y = contentPadding;
            listHolder.x = _loc_27;
            contentScrollRect = listHolder.scrollRect;
            contentScrollRect.x = Math.floor(_horizontalScrollPosition) % _loc_7;
            contentScrollRect.y = Math.floor(_verticalScrollPosition) % _loc_8;
            listHolder.scrollRect = contentScrollRect;
            listHolder.cacheAsBitmap = useBitmapScrolling;
            var _loc_13:Array = [];
            if (_scrollDirection == ScrollBarDirection.HORIZONTAL)
            {
                _loc_16 = availableWidth / _loc_7 << 0;
                if (this._isUseFixed)
                {
                    _loc_16 = (availableWidth + this.horizontalGap) / (_loc_7 + this.horizontalGap) << 0;
                }
                _loc_17 = Math.max(_loc_16, Math.ceil(length / _loc_5));
                _loc_9 = _horizontalScrollPosition / _loc_7 << 0;
                if (this._isUseFixed)
                {
                    _loc_9 = _horizontalScrollPosition / (_loc_7 + this.horizontalGap) << 0;
                }
                _loc_6 = Math.max(_loc_16, Math.min(_loc_17 - _loc_9, (_loc_6 + 1)));
                _loc_12 = 0;
                while (_loc_12 < _loc_5)
                {
                    
                    _loc_11 = 0;
                    while (_loc_11 < _loc_6)
                    {
                        
                        _loc_2 = _loc_12 * _loc_17 + _loc_9 + _loc_11;
                        if (_loc_2 >= length)
                        {
                            break;
                        }
                        _loc_13.push(_loc_2);
                        _loc_11 = _loc_11 + 1;
                    }
                    _loc_12 = _loc_12 + 1;
                }
            }
            else
            {
                _loc_5 = _loc_5 + 1;
                _loc_10 = _verticalScrollPosition / _loc_8 << 0;
                if (this._isUseFixed)
                {
                    _loc_10 = _verticalScrollPosition / (_loc_8 + this.verticalGap) << 0;
                }
                _loc_18 = Math.floor(_loc_10 * _loc_6);
                _loc_19 = Math.min(length, _loc_18 + _loc_5 * _loc_6);
                _loc_1 = _loc_18;
                while (_loc_1 < _loc_19)
                {
                    
                    _loc_13.push(_loc_1);
                    _loc_1 = _loc_1 + 1;
                }
            }
            var _loc_27:* = new Dictionary(true);
            renderedItems = new Dictionary(true);
            var _loc_14:* = _loc_27;
            for each (_loc_2 in _loc_13)
            {
                
                _loc_14[_dataProvider.getItemAt(_loc_2)] = true;
            }
            _loc_15 = new Dictionary(true);
            while (activeCellRenderers.length > 0)
            {
                
                _loc_4 = activeCellRenderers.pop();
                _loc_3 = _loc_4.data;
                if (_loc_14[_loc_3] == null || invalidItems[_loc_3] == true)
                {
                    availableCellRenderers.push(_loc_4);
                    list.removeChild(_loc_4 as DisplayObject);
                    continue;
                }
                _loc_15[_loc_3] = _loc_4;
                invalidItems[_loc_3] = true;
            }
            invalidItems = new Dictionary(true);
            _loc_1 = 0;
            for each (_loc_2 in _loc_13)
            {
                
                _loc_11 = _loc_1 % _loc_6;
                _loc_12 = _loc_1 / _loc_6 << 0;
                _loc_20 = false;
                _loc_3 = _dataProvider.getItemAt(_loc_2);
                if (_loc_15[_loc_3] != null)
                {
                    _loc_20 = true;
                    _loc_4 = _loc_15[_loc_3];
                    delete _loc_15[_loc_3];
                }
                else if (availableCellRenderers.length > 0)
                {
                    _loc_4 = availableCellRenderers.pop() as ICellRenderer;
                }
                else
                {
                    _loc_4 = this.getCellRenderer(getStyleValue("cellRenderer"));
                    _loc_24 = _loc_4 as Sprite;
                    if (_loc_24 != null)
                    {
                        _loc_24.addEventListener(MouseEvent.CLICK, this.handleCellRendererClick, false, 0, true);
                        _loc_24.addEventListener(MouseEvent.ROLL_OVER, handleCellRendererMouseEvent, false, 0, true);
                        _loc_24.addEventListener(MouseEvent.ROLL_OUT, handleCellRendererMouseEvent, false, 0, true);
                        _loc_24.addEventListener(Event.CHANGE, this.handleCellRendererChange, false, 0, true);
                        _loc_24.doubleClickEnabled = true;
                        _loc_24.addEventListener(MouseEvent.DOUBLE_CLICK, handleCellRendererDoubleClick, false, 0, true);
                        if (_loc_24.hasOwnProperty("setStyle"))
                        {
                            for (_loc_25 in rendererStyles)
                            {
                                
                                var _loc_31:* = _loc_24;
                                _loc_31._loc_24["setStyle"](_loc_25, rendererStyles[_loc_25]);
                            }
                        }
                    }
                }
                list.addChild(_loc_4 as Sprite);
                activeCellRenderers.push(_loc_4);
                _loc_4.y = _loc_8 * _loc_12 + this.getTotalGap(_loc_12, this._arrVGap, this._verticalGap);
                _loc_4.x = _loc_7 * _loc_11 + this.getTotalGap(_loc_11, this._arrHGap, this._horizontalGap);
                _loc_4.setSize(columnWidth, rowHeight);
                _loc_21 = this.itemToLabel(_loc_3);
                _loc_22 = null;
                if (_iconFunction != null)
                {
                    _loc_22 = _iconFunction(_loc_3);
                }
                else if (_iconField != null && _loc_3 && _loc_3.hasOwnProperty(_iconField))
                {
                    _loc_22 = _loc_3[_iconField];
                }
                _loc_23 = null;
                if (_sourceFunction != null)
                {
                    _loc_23 = _sourceFunction(_loc_3);
                }
                else if (_sourceField != null && _loc_3 && _loc_3.hasOwnProperty(_sourceField))
                {
                    _loc_23 = _loc_3[_sourceField];
                }
                if (!_loc_20)
                {
                    _loc_4.data = _loc_3;
                }
                _loc_4.listData = new TileListData(_loc_21, _loc_22, _loc_23, this, _loc_2, _loc_10 + _loc_12, _loc_9 + _loc_11) as ListData;
                _loc_4.selected = _selectedIndices.indexOf(_loc_2) != -1;
                if (_loc_4 is UIComponent)
                {
                    _loc_26 = _loc_4 as UIComponent;
                    _loc_26.drawNow();
                }
                _loc_1 = _loc_1 + 1;
            }
            return;
        }// end function

        protected function getCellRenderer(param1:Object) : ICellRenderer
        {
            var _loc_2:DisplayObject = null;
            if (param1 is Class)
            {
                _loc_2 = UICompomentPool.getUICompoment(param1 as Class) as DisplayObject;
                if (_loc_2 is IFrUIContainer)
                {
                    (_loc_2 as IFrUIContainer).createDisposedChildren();
                }
                if (_loc_2 is UIComponent)
                {
                    (_loc_2 as UIComponent).focusEnabled = false;
                    (_loc_2 as UIComponent).mouseFocusEnabled = false;
                    (_loc_2 as UIComponent).tabChildren = false;
                    (_loc_2 as UIComponent).tabEnabled = false;
                }
                return _loc_2 as ICellRenderer;
            }
            return getDisplayObjectInstance(param1) as ICellRenderer;
        }// end function

        override public function set dataProvider(param1:DataProvider) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:Object = null;
            var _loc_5:Object = null;
            if (super.dataProvider)
            {
                _loc_2 = Math.max(super.dataProvider.length, param1.length);
                _loc_3 = 0;
                while (_loc_3 < _loc_2)
                {
                    
                    if (_loc_3 > (super.dataProvider.length - 1) && _loc_3 <= (param1.length - 1))
                    {
                        super.dataProvider.addItemAt(param1.getItemAt(_loc_3), _loc_3);
                    }
                    else if (_loc_3 > (param1.length - 1))
                    {
                        super.dataProvider.removeItemAt(param1.length);
                    }
                    else
                    {
                        _loc_4 = this.dataProvider.getItemAt(_loc_3);
                        _loc_5 = param1.getItemAt(_loc_3);
                        super.dataProvider.replaceItemAt(_loc_5, _loc_3);
                        invalidateItemAt(_loc_3);
                    }
                    _loc_3++;
                }
            }
            else
            {
                super.dataProvider = param1;
            }
            return;
        }// end function

        override public function itemToLabel(param1:Object) : String
        {
            if (_labelFunction != null)
            {
                return String(_labelFunction(param1));
            }
            if (_labelField == null || param1 == null || param1.hasOwnProperty(_labelField) == false)
            {
                return "";
            }
            return String(param1[_labelField]);
        }// end function

        override protected function _invalidateList() : void
        {
            var _loc_1:int = 0;
            if (this._isDraw)
            {
                super._invalidateList();
            }
            else
            {
                _loc_1 = 0;
                while (_loc_1 < _dataProvider.length)
                {
                    
                    invalidateItem(_dataProvider.getItemAt(_loc_1));
                    _loc_1++;
                }
                invalidate(InvalidationType.DATA);
            }
            return;
        }// end function

        override protected function keyDownHandler(event:KeyboardEvent) : void
        {
            return;
        }// end function

        override protected function keyUpHandler(event:KeyboardEvent) : void
        {
            return;
        }// end function

        public function configEventListener(param1:String, param2:Function, param3:Boolean = false, param4:int = 0, param5:Boolean = false) : void
        {
            ObjEventListerTool.addObjEvent(this, param1, param2, param3);
            addEventListener(param1, param2, param3, param4, param5);
            return;
        }// end function

        public function set isDisposed(param1:Boolean) : void
        {
            this._isDisposed = param1;
            return;
        }// end function

        public function get isDisposed() : Boolean
        {
            return this._isDisposed;
        }// end function

        override public function removeEventListener(param1:String, param2:Function, param3:Boolean = false) : void
        {
            ObjEventListerTool.removeObjEvent(this, param1, param2, param3);
            super.removeEventListener(param1, param2, param3);
            return;
        }// end function

        public function disposeRender() : void
        {
            var _loc_1:ICellRenderer = null;
            if (_dataProvider)
            {
                _dataProvider.removeAll();
            }
            while (activeCellRenderers.length > 0)
            {
                
                _loc_1 = activeCellRenderers.pop();
                this.removeRenderEventListener(_loc_1 as Sprite);
                if (_loc_1 is IDispose)
                {
                    (_loc_1 as IDispose).dispose();
                    continue;
                }
                list.removeChild(_loc_1 as DisplayObject);
            }
            while (availableCellRenderers.length > 0)
            {
                
                _loc_1 = availableCellRenderers.pop();
                this.removeRenderEventListener(_loc_1 as Sprite);
                if (_loc_1 is IDispose)
                {
                    (_loc_1 as IDispose).dispose();
                    continue;
                }
                if ((_loc_1 as DisplayObject).parent)
                {
                    (_loc_1 as DisplayObject).parent.removeChild(_loc_1 as DisplayObject);
                }
            }
            return;
        }// end function

        protected function removeRenderEventListener(param1:Sprite) : void
        {
            if (!param1)
            {
                return;
            }
            param1.removeEventListener(MouseEvent.CLICK, this.handleCellRendererClick, false);
            param1.removeEventListener(MouseEvent.ROLL_OVER, handleCellRendererMouseEvent, false);
            param1.removeEventListener(MouseEvent.ROLL_OUT, handleCellRendererMouseEvent, false);
            param1.removeEventListener(Event.CHANGE, this.handleCellRendererChange, false);
            param1.doubleClickEnabled = false;
            param1.removeEventListener(MouseEvent.DOUBLE_CLICK, handleCellRendererDoubleClick, false);
            return;
        }// end function

        override protected function handleCellRendererClick(event:MouseEvent) : void
        {
            if (!this.isClickedUpdate)
            {
                return;
            }
            super.handleCellRendererClick(event);
            return;
        }// end function

        override protected function handleCellRendererChange(event:Event) : void
        {
            if (!this.isChangeUpdate)
            {
                return;
            }
            super.handleCellRendererChange(event);
            return;
        }// end function

        protected function createChildren() : void
        {
            return;
        }// end function

        protected function updateStyle() : void
        {
            return;
        }// end function

        protected function updateSize() : void
        {
            return;
        }// end function

        protected function updateDate() : void
        {
            return;
        }// end function

        protected function updateDisplayList() : void
        {
            return;
        }// end function

        public function dispose(param1:Boolean = true) : void
        {
            if (this._isDisposed && param1)
            {
                if (Global.isDebugModle)
                {
                    Alert.show("存在组件二次释放" + this.toString());
                }
                return;
            }
            this._isCenter = false;
            this._isUseFixed = false;
            this._initialX = 0;
            this.disposeRender();
            renderedItems = new Dictionary(true);
            invalidItems = new Dictionary(true);
            _horizontalScrollPosition = 0;
            _verticalScrollPosition = 0;
            ObjEventListerTool.delObjEvent(this);
            this.isClickedUpdate = true;
            this.isChangeUpdate = true;
            this.isShowVerticalScrollBar = false;
            this.mouseEnabled = true;
            this.mouseChildren = true;
            this.selectable = true;
            if (this.parent)
            {
                this.parent.removeChild(this);
            }
            if (param1)
            {
                UICompomentPool.disposeUICompoment(this);
            }
            return;
        }// end function

    }
}
